package exceltocard;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import exceltocard.AppendVCard;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Maincard {
	public static boolean exceltocard(File excel, File vcard, int namecell, int numbercell) {
		AppendVCard avc = new AppendVCard(vcard.getAbsolutePath());
		FileInputStream fIP;
		try {
			String name = "";
			String number = "";
			namecell=1;
			numbercell=2;
			String filename = excel.getName();
			String type = filename.substring(filename.lastIndexOf(".")+1);
			String cardfile= vcard.getName();
			String vctype = cardfile.substring(cardfile.lastIndexOf(".")+1);
//			if(type.length()>3){
//				type=type.substring(0,3);
//			}
			switch (type) {
				case "xlsx":
				case "xls":
					fIP = new FileInputStream(excel);
					Sheet spreadsheet =null;
					FormulaEvaluator objFormulaEvaluator = null;
					if("xlsx".equals(type)) {
						XSSFWorkbook XSSFWorkbook = new XSSFWorkbook(fIP);
						spreadsheet = XSSFWorkbook.getSheetAt(0);
						objFormulaEvaluator = new XSSFFormulaEvaluator(XSSFWorkbook);
					}
					else {
						HSSFWorkbook HSSFworkbook = new HSSFWorkbook(fIP);
						spreadsheet = HSSFworkbook.getSheetAt(0);
						objFormulaEvaluator = new HSSFFormulaEvaluator(HSSFworkbook);
					}
					DataFormatter objDefaultFormat = new DataFormatter();
					Iterator<Row> rowIterator = spreadsheet.iterator();
					while (rowIterator.hasNext()) {
						Row row = rowIterator.next();
						name = row.getCell(namecell - 1).toString();
						Cell cellValue = row.getCell(numbercell - 1);
						objFormulaEvaluator.evaluate(cellValue);
						number = objDefaultFormat.formatCellValue(cellValue, objFormulaEvaluator);
						avc.appendFile(name, number);
					}
					break;
				case "csv":
					if(vctype.contains("xls")){
						CsvToExcel.ConvertorToExcel(excel,vcard);
					}
					DataInputStream in = new DataInputStream(new FileInputStream(excel));
						// 编码
						byte[] b = new byte[3];
						in.read(b);
						in.close();
						String code;
						if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
							code = "UTF-8";
						} else {
							code = "GBK";
						}
						in.close();
						in = new DataInputStream(new FileInputStream(excel));
					BufferedReader br = new BufferedReader(new InputStreamReader(in, code));

					CSVParser records = CSVFormat.EXCEL.parse(br);

					List<CSVRecord> iterator = records.getRecords();
					// 先处理表头
					CSVRecord titleRecord = iterator.get(0);
					int titleCount = titleRecord.size();
					List<String> list = new ArrayList<String>(titleCount);
					for (int j = 0; j < titleCount; j++) {
						list.add(titleRecord.get(j));
					}
					// 开始处理value
					List<String>[] values = new ArrayList[titleCount];
					// 初始化数组
					for (int i = 0; i < values.length; i++) {
						List<String> value = new ArrayList<String>();
						values[i] = value;
					}
					// 循环读取value
					for (int i = 0; i < iterator.size(); i++) {
						CSVRecord csvRecord = iterator.get(i);
						int count = csvRecord.size();
						for (int j = 0; j < count; j++) {
							List<String> value = values[j];
							value.add(csvRecord.get(j));
							values[j] = value;
						}
						name = csvRecord.get(0);
						number = csvRecord.get(numbercell - 1);
						avc.appendFile(name, number);
					}
					break;
				case "vcf":
					CsvToExcel.ConvertorToExcel(excel,vcard);
					break;
			}
				System.out.println("转换完成。");
				return true;
		}
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
}
